package cn.linkai.hadoop.hdfs;

import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class HDFSPlay {

    Configuration conf;
    FileSystem fileSystem;

    @Before
    public void before() throws Exception {
        conf=new Configuration();
        conf.set("fs.defaultFS","hdfs://hadoop1:9000");
        System.setProperty("hadoop.home.dir","D:\\hadoop-2.9.1");
        System.setProperty("HADOOP_HOME","D:\\hadoop-2.9.1");
        fileSystem=FileSystem.newInstance(new URI("hdfs://hadoop1:9000"),conf,"hadoop");
    }

    @Test
    public void testUpload() throws IOException {
        fileSystem.copyFromLocalFile(new Path("D:\\googledownload\\mysql-5.7.22-winx64.zip"),new Path("/"));
    }

    @Test
    public void testRename() throws IOException {
        fileSystem.rename(new Path("/mysql-5.7.22-winx64.zip"),new Path("/mysql.zip"));
    }

    @Test
    public void testDownload() throws IOException {
        fileSystem.copyToLocalFile(new Path("/mysql.zip"),new Path("E:\\"));
    }

    @Test
    public void mkdir() throws IOException {
        fileSystem.mkdirs(new Path("/hdfstest"));
    }

    @Test
    public void list() throws IOException {
        FileStatus[] fileStatuses = fileSystem.listStatus(new Path("/"));
        for (FileStatus fileStatus:fileStatuses) {
            System.out.println(fileStatus.getPath().toString());
        }
    }

    @Test
    public void uploadByStream() throws IOException {
        FSDataOutputStream fsDataOutputStream = fileSystem.create(new Path("/hdfstest/hadoop"), true);
        FileInputStream in=new FileInputStream("D:\\googledownload\\hadoop-2.9.1.tar.gz");
        IOUtils.copy(in,fsDataOutputStream);
    }

    @Test
    public void downloadByStream() throws IOException {
        FSDataInputStream fsDataInputStream = fileSystem.open(new Path("/hdfstest/hadoop"));
        FileOutputStream outputStream=new FileOutputStream("E:\\hadoop");
        IOUtils.copy(fsDataInputStream,outputStream);

    }
    @Test
    public void delete() throws IOException {
        fileSystem.delete(new Path("/hdfstest"),true);
    }

    @After
    public void after() throws IOException {
        fileSystem.close();
    }
}
